﻿using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;

namespace S.Common.Extensions
{
    //
    // 摘要:
    //     Provides a set of static (Shared in Visual Basic) methods for querying objects
    //     that implement System.Collections.Generic.IEnumerable`1.
    //遍历树
    public static class Enumerables
    {
        public static IEnumerable<T> Traverse<T>(this IEnumerable<T> source, Func<T, IEnumerable<T>> childrenSelector)
        {
            foreach (var item in source)
            {
                yield return item;

                var children = childrenSelector(item);
                if (children != null)
                {
                    foreach (var child in children.Traverse(childrenSelector))
                    {
                        yield return child;
                    }
                }
            }
        }
    }
}